import com.troyworks.framework.ui.DraggableClip;
import com.troyworks.framework.ui.layout.CollisionDetection;
import com.troyworks.framework.ui.layout.BoundsRelationship;
import com.troyworks.framework.ui.layout.Bounds2;
import flash.geom.Matrix;
/**
 * @author Troy Gardner
 */
class com.troyworks.framework.ui.SnapToTest extends MovieClip {
	public var numOfParticles : Number = 2;

	private var lines_mc : MovieClip;
	public var clips : Array = new Array();
	public var canSnapToLeft : Boolean = true;
	public var canSnapToRight : Boolean = true;
	public var canSnapToTop : Boolean = true;
	public var canSnapToBottom : Boolean = true;
	public var anim:Number = 0;
	public function onLoad() : Void{

		for (var i : Number = 0; i < numOfParticles; i++) {
			var clip : DraggableClip = DraggableClip(attachMovie("Rect", i+"Rect", getNextHighestDepth(), {id:i}));
			clip._x = (Stage.width - clip._width) * Math.random();
			clip._y = (Stage.height- clip._height) * Math.random();
			clips.push(clip);

		}
		DraggableClip.selectionMan.topClipDepth = getNextHighestDepth();
		lines_mc = createEmptyMovieClip("line_mc", 10002);	
	}

	function checkCollisionOfBounds(clipA : MovieClip, clipB : MovieClip) : Void{
		var snapToDist : Number = 60;

		var Ab : Bounds2 = Bounds2.getBounds(clipA, this);
		var Abr : Bounds2 = Bounds2.getBounds(clipA, this);
		var Bb : Bounds2 = Bounds2.getBounds(clipB, this);
		Ab.inflateBy(snapToDist);

		var A_relationShipToB : BoundsRelationship = CollisionDetection.getBoundsRelationship(Ab, Bb);

		var dx : Number = 0;
		var dy : Number = 0;
		if(canSnapToRight && (A_relationShipToB.isRightOf || A_relationShipToB.isOnRightSideOf) ){
		//	trace("RIGHT OF");
			dx = (Bb.xMax-Abr.xMin);
			dy = (Bb.yMid-Abr.yMid);
			//drawLine(Bb.xMax, Bb.yMid, Abr.xMin, Abr.yMid);
		}else if(canSnapToLeft && (A_relationShipToB.isLeftOf || A_relationShipToB.isOnLeftSideOf)){
		//	trace("LEFT OF");
			dx = (Abr.xMax-Bb.xMin);
			dy = (Bb.yMid-Abr.yMid);
			//drawLine(Bb.xMin, Bb.yMid, Abr.xMax, Abr.yMid);
		}else if(canSnapToTop && (A_relationShipToB.isAboveOf || A_relationShipToB.isOnTopSideOf)){
		//	trace("ABOVE OF");
			dx = (Abr.xMid-Bb.xMid);
			dy = (Bb.yMin-Abr.yMax);
			//drawLine(Bb.xMid, Bb.yMin, Abr.xMid, Abr.yMax);
				
		}else if(canSnapToBottom && (A_relationShipToB.isBelowOf || A_relationShipToB.isOnBottomSideOf)){
		//	trace("BELOW OF");
			dx = (Abr.xMid-Bb.xMid);
			dy = (Bb.yMax-Abr.yMin);
			//drawLine(Bb.xMid, Bb.yMax, Abr.xMid, Abr.yMin);
				
		}else{
			trace("?????1");
		}
		var dist : Number = Math.sqrt(dx*dx + dy*dy);

		//Ab.visualize(lines_mc);
		//Bb.visualize(lines_mc);
		if (A_relationShipToB.isOverlapping && (dist <snapToDist)) {
			var color : Number = 0x666666;
			var alpha : Number = (1-(Math.min(dist,snapToDist*.6))/snapToDist*.6)*100;
			trace("INNER LOOP alpha " + dist + " " + snapToDist + " =  " + alpha);
		if(canSnapToRight && A_relationShipToB.isOnRightSideOf){
			//trace("RIGHT OF2");
			
			/////draw lines/////////////////
				lines_mc.moveTo(Abr.xMin, Abr.yMin);
				lines_mc.lineStyle(1, color,alpha);
				lines_mc.lineTo(Bb.xMax, Bb.yMin);
				lines_mc.moveTo(Abr.xMin, Abr.yMax);
				lines_mc.lineTo(Bb.xMax, Bb.yMax);
				if(clipA.isSelected){
					clipA.tX = Bb.xMax;
					clipA.tY = Bb.yMin;

					drawPoint(Bb.xMax,Bb.yMin, color,alpha);
					drawPoint(Bb.xMax,Bb.yMax, color,alpha);			
				}else if(clipB.isSelected){
					clipB.tX = Abr.xMin - Bb.width;
					clipB.tY = Abr.yMin;
					drawPoint(Abr.xMin,Abr.yMin, color,alpha);
					drawPoint(Abr.xMin,Abr.yMax, color,alpha);
				}
			
			/////draw points /////////////////

		}else if(canSnapToLeft && A_relationShipToB.isOnLeftSideOf){
			//trace("LEFT OF2");
	
			/////draw lines/////////////////
				lines_mc.moveTo(Abr.xMax, Abr.yMin);
				lines_mc.lineStyle(1, color,alpha);
				lines_mc.lineTo(Bb.xMin, Bb.yMin);
				lines_mc.moveTo(Abr.xMax, Abr.yMax);
				lines_mc.lineTo(Bb.xMin, Bb.yMax);
				if(clipA.isSelected){
					clipA.tX = Bb.xMin - Abr.width;
					clipA.tY = Bb.yMin;

					drawPoint(Bb.xMin,Bb.yMin, color,alpha);
					drawPoint(Bb.xMin,Bb.yMax, color,alpha);			
					}else if(clipB.isSelected){
					clipB.tX = Abr.xMax;
					clipB.tY = Abr.yMin;
								/////draw points /////////////////
					drawPoint(Abr.xMax,Abr.yMin, color,alpha);
					drawPoint(Abr.xMax,Abr.yMax, color,alpha);
				}
		}else if(canSnapToTop && A_relationShipToB.isOnTopSideOf){
	//		 trace("ABOVE OF2");
			/////draw lines/////////////////
				lines_mc.lineStyle(1, color,alpha);
				lines_mc.moveTo(Abr.xMin, Abr.yMax);
				lines_mc.lineTo(Bb.xMin, Bb.yMin);
				lines_mc.moveTo(Abr.xMax, Abr.yMax);
				lines_mc.lineTo(Bb.xMax, Bb.yMin);
				endFill();
					
				if(clipA.isSelected){
					clipA.tX = Bb.xMin;
					clipA.tY = Bb.yMin - Abr.height;
								/////draw points /////////////////
					drawPoint(Bb.xMin,Bb.yMin, color,alpha);
					drawPoint(Bb.xMax,Bb.yMin, color,alpha);
					
					}else if(clipB.isSelected){
					clipB.tX = Abr.xMin;
					clipB.tY = Abr.yMax;
					drawPoint(Abr.xMin,Abr.yMax, color,alpha);
					drawPoint(Abr.xMax,Abr.yMax, color,alpha);
					
				}
				
		}else if(canSnapToBottom && A_relationShipToB.isOnBottomSideOf){
	//		trace("BELOW OF2");
			/////draw lines/////////////////
				lines_mc.lineStyle(1, color,alpha);
				if(false){
					lines_mc.beginFill(color,alpha);
					lines_mc.moveTo(Abr.xMin, Abr.yMin);
					lines_mc.lineTo(Bb.xMin, Bb.yMax);
					lines_mc.moveTo(Abr.xMax, Abr.yMin);
					lines_mc.lineTo(Bb.xMax, Bb.yMax);
				}else{
					trace("filling in square");
					var fillType:String = "linear";
					var colors:Array = [0xFF0000, 0x0000FF];
					var alphas:Array = [100, 100];
					var ratios:Array = [0x00, 0xFF];
					var matrix:Matrix = new Matrix();
					matrix.createGradientBox(20,dist, 0, 0,0);
					matrix.tx = anim;
					matrix.rotate(90/180* Math.PI);
					var spreadMethod:String = "repeat";
					lines_mc.beginGradientFill(fillType, colors, alphas, ratios, matrix, spreadMethod); 
					
					//lines_mc.beginFill(color,alpha);
				//	var matrix:Matrix = new Matrix();
    				//matrix.createGradientBox(Abr.width, Abr.height,0,0,0);
 
				//	lines_mc.beginGradientFill("linear",[color,0xCCCCCC],[100,100],[0,255]);
					lines_mc.moveTo(Abr.xMin, Abr.yMin);
					lines_mc.lineTo(Bb.xMin, Bb.yMax);
					lines_mc.lineTo(Bb.xMax, Bb.yMax);
					lines_mc.lineTo(Abr.xMax, Abr.yMin);
					lines_mc.lineTo(Abr.xMin, Abr.yMin);
					lines_mc.endFill();
				}
				endFill();
				
				if(clipA.isSelected){
					clipA.tX = Bb.xMin;
					clipA.tY = Bb.yMax;
					anim--;
					drawPoint(Bb.xMin,Bb.yMax, color,alpha);
					drawPoint(Bb.xMax,Bb.yMax, color,alpha);
				}else if(clipB.isSelected){
					clipB.tX = Abr.xMin;
					clipB.tY = Abr.yMin - Bb.height;
					anim++;
					drawPoint(Abr.xMin,Abr.yMin, color,alpha);
					drawPoint(Abr.xMax,Abr.yMin, color,alpha);						
					
				}
			/////draw points /////////////////
		}else{
				trace("????");
				clipA.tX = null;
				clipA.tY = null;
				clipB.tX = null;
				clipB.tY = null;
		}
		
	
		}else{
			clipA.tX = null;
			clipA.tY = null;
				clipB.tX = null;
				clipB.tY = null;
	
		}
		
	}
	function drawLine(x : Number, y : Number, x2 : Number, y2 : Number) : Void{
		lines_mc.lineStyle(5, 0xFF0000, 100);
		lines_mc.moveTo(x, y);
		lines_mc.lineTo(x2, y2);
	}
	function drawPoint(x : Number, y : Number, color : Number, alpha : Number) : Void{
		lines_mc.lineStyle(5, color, alpha);
		lines_mc.moveTo(x, y);
		lines_mc.lineTo(x+0.2, y);
	}
	function drawRect(x : Number, y : Number, x2 : Number, y2 : Number, color : Number, alpha : Number) : Void{
		lines_mc.beginFill(color,alpha);
		lines_mc.moveTo(x, y);
		lines_mc.lineTo(x2, y);
		lines_mc.lineTo(x2, y2);
		lines_mc.lineTo(x, y2);
		lines_mc.lineTo(x, y);
		lines_mc.endFill();
	}

	function onEnterFrame() : Void {

		lines_mc.clear();
		trace("frame----------------------------------------------------");
		for(var i = 0;i<numOfParticles-1;i++)
		{
			var partA : MovieClip = clips[i];
			for(var j : Number = i+1;j<numOfParticles;j++)
			{
				var partB : MovieClip = clips[j];
				checkCollisionOfBounds(partA, partB);
			}
		}

}
	
}